toast('启动脚本');
auto.waitFor();

if(!requestScreenCapture(true)){
    toast("请求截图失败");
    exit();
}
launch("com.woniu.mobile9yin");

//登录页面
id("signin_button").waitFor();
id("signin_button").click();
sleep(1000);

// 选区
id("title_text").text("服务器大区").waitFor();
text("江湖六区").findOne().parent().click();
sleep(1000);

while(!id("news_btn").findOnce()){
    text("举世无双").findOne().parent().click();
    sleep(1000);
}

while(true){
    handleCitan()
}


function handleCitan(){
    // 主页面
    id("citan_btn").findOne().click();
    sleep(1000);

    id("item_citan_btn").findOne().click();
    sleep(1000);

    // 刺探页面
    var count = text("当天刺探剩余次数：").findOne().parent().child(1).text();
    count = Number(count); // 当天剩余次数
    if(count == 5){
        toast("今日完成");
        exit();
    }


    findCitan();


    // 找到需要点击的刺探
    function findCitan(){
        while(true){
            var tempProgressList  = textContains("进度").find();
            var currentBlock = null; // 即将刺探
            tempProgressList.forEach(element => {
                if(element.text() != "进度:4/4"){
                    currentBlock = element.parent().child(0);
                }
            });
            if(currentBlock){
                currentBlock.click();
                sleep(1000);
                id("citan_danci").findOne().click()
                break;
            }
            else{
                sleep(3000);
                if(tempProgressList.length == 0){
                    back();
                    handleCitan();
                }
                else{
                    continue;
                }
            }      
        }
    }


    // 开始刺探界面
    var btnRectList = []; // 金木水火土的Rect
    btnRectList.push(id("item_jing").findOne().bounds());
    btnRectList.push(id("item_shui").findOne().bounds());
    btnRectList.push(id("item_tu").findOne().bounds());
    btnRectList.push(id("item_mu").findOne().bounds());
    btnRectList.push(id("item_huo").findOne().bounds());
    console.log(btnRectList);


    id("citan_item_start_btn").findOne().click();

    var orderList = []
    var flag = 0
    var requireOrder = []
    while(true){
        let capture = captureScreen();
        // capture.saveTo("/sdcard/screencapture/"+flag+".png")
        flag++;
        console.log(flag);
        for(let i = 0; i < 5; i++){
            let region = [btnRectList[i].left, btnRectList[i].top, btnRectList[i].width(), btnRectList[i].height()];
            let point = images.findColor(capture, "#EFD905",{
                region : region,
                threshold: 3
            });
            if(point){
                orderList.push(i);
            }
        }
        if(text("提醒").findOnce()){
            let tempText = textContains("需要点击的按钮是").findOne().text();
            requireOrder.push(Number(tempText.slice(8,9))-1);
            requireOrder.push(Number(tempText.slice(10,11))-1);
            requireOrder.push(Number(tempText.slice(12,13))-1);
            console.log(requireOrder);
            sleep(1000);
            break;
        }


    }
    // 去重
    function unique(arr) {
        if (!Array.isArray(arr)) {
            console.log('type error!')
            return
        }
        var array = [];
        for (var i = 0; i < arr.length; i++) {
            if (array .indexOf(arr[i]) === -1) {
                array .push(arr[i])
            }
        }
        return array;
    }
    var orderListNew = unique(orderList);
    console.log(orderListNew);

    text("确认").findOne().click();
    sleep(1000);

    // 点击对应块
    for(let i = 0 ; i < 3 ; i++){
        let tempNum = orderListNew[requireOrder[i]];
        console.log(tempNum);
        switch(tempNum){
            case 0:
                id("item_jing").findOne().click();
                break;
            case 1:
                id("item_shui").findOne().click();
                break;
            case 2:
                id("item_tu").findOne().click();
                break;
            case 3:
                id("item_mu").findOne().click();
                break;
            case 4:
                id("item_huo").findOne().click();
                break;
            
        }
        sleep(500);
    }
    id("item_submit").findOne().click();
    sleep(1000);
    text("确认").findOne().click();

}


toast("脚本结束");
